//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class LocaleStringResourceDBManager
    {
        #region Methods
        public static void DeleteLocaleStringResource(int LocaleStringResourceID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LocaleStringResourceDelete");
            db.AddInParameter(dbCommand, "LocaleStringResourceID", DbType.Int32, LocaleStringResourceID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static LocaleStringResource GetByLocaleStringResourceID(int LocaleStringResourceID)
        {
            LocaleStringResource localeStringResource = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LocaleStringResourceLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "LocaleStringResourceID", DbType.Int32, LocaleStringResourceID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    localeStringResource = new LocaleStringResource();
                    localeStringResource.LocaleStringResourceID = NopDataHelper.GetInt(dataReader, "LocaleStringResourceID");
                    localeStringResource.LanguageID = NopDataHelper.GetInt(dataReader, "LanguageID");
                    localeStringResource.ResourceName = NopDataHelper.GetString(dataReader, "ResourceName");
                    localeStringResource.ResourceValue = NopDataHelper.GetString(dataReader, "ResourceValue");
                }
            }
            return localeStringResource;
        }

        public static LocaleStringResourceCollection GetAllResourcesByLanguageID(int LanguageID)
        {
            LocaleStringResourceCollection localeStringResourceCollection = new LocaleStringResourceCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LocaleStringResourceLoadAllByLanguageID");
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, LanguageID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    LocaleStringResource localeStringResource = new LocaleStringResource();
                    localeStringResource.LocaleStringResourceID = NopDataHelper.GetInt(dataReader, "LocaleStringResourceID");
                    localeStringResource.LanguageID = NopDataHelper.GetInt(dataReader, "LanguageID");
                    localeStringResource.ResourceName = NopDataHelper.GetString(dataReader, "ResourceName");
                    localeStringResource.ResourceValue = NopDataHelper.GetString(dataReader, "ResourceValue");
                    localeStringResourceCollection.Add(localeStringResource);
                }
            }

            return localeStringResourceCollection;
        }

        public static LocaleStringResource InsertLocaleStringResource(int LanguageID, string ResourceName, string ResourceValue)
        {
            LocaleStringResource localeStringResource = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LocaleStringResourceInsert");
            db.AddOutParameter(dbCommand, "LocaleStringResourceID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, LanguageID);
            db.AddInParameter(dbCommand, "ResourceName", DbType.String, ResourceName);
            db.AddInParameter(dbCommand, "ResourceValue", DbType.String, ResourceValue);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int LocaleStringResourceID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@LocaleStringResourceID"));
                localeStringResource = GetByLocaleStringResourceID(LocaleStringResourceID);
            }
            return localeStringResource;
        }

        public static LocaleStringResource UpdateLocaleStringResource(int LocaleStringResourceID, int LanguageID, string ResourceName, string ResourceValue)
        {
            LocaleStringResource localeStringResource = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LocaleStringResourceUpdate");
            db.AddInParameter(dbCommand, "LocaleStringResourceID", DbType.Int32, LocaleStringResourceID);
            db.AddInParameter(dbCommand, "LanguageID", DbType.Int32, LanguageID);
            db.AddInParameter(dbCommand, "ResourceName", DbType.String, ResourceName);
            db.AddInParameter(dbCommand, "ResourceValue", DbType.String, ResourceValue);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                localeStringResource = GetByLocaleStringResourceID(LocaleStringResourceID);

            return localeStringResource;
        }
        #endregion
    }
}
